const fs = require('fs');
const path = require('path');

const packageDir = path.join(__dirname, 'package'); // 指向你的 package 目录
const outputFile = path.join(__dirname, 'pages.json');

const mainPages = [];
const subPackages = [];

// 读取主包的 pages.json
function readMainPages() {
    const mainDir = path.join(packageDir, 'main', 'pages.json');
    if (fs.existsSync(mainDir)) {
        const mainConfig = JSON.parse(fs.readFileSync(mainDir, 'utf-8'));
        mainConfig.pages.forEach(page => {
            mainPages.push({
                path: `package/main/${page.path}`, // 自动生成前缀
                style: page.style
            });
        });
    }
}

// 读取子包的 pages.json
function readSubPackages() {
    const items = fs.readdirSync(packageDir);

    items.forEach(item => {
        const fullPath = path.join(packageDir, item);
        const stat = fs.statSync(fullPath);

        if (stat.isDirectory() && item !== 'main') {
            const subPackageConfigPath = path.join(fullPath, 'pages.json');
            if (fs.existsSync(subPackageConfigPath)) {
                const subPackageConfig = JSON.parse(fs.readFileSync(subPackageConfigPath, 'utf-8'));
                const subPackagePages = subPackageConfig.pages.map(page => ({
                    path: `${page.path}`, // 自动生成前缀
                    style: page.style
                }));

                subPackages.push({
                    root: 'package/' +item,
                    pages: subPackagePages
                });
            }
        }
    });
}

// 读取主包和子包的配置
readMainPages();
readSubPackages();

// 生成最终的 pages.json
const finalConfig = {
    pages: mainPages,
    subPackages: subPackages,
	"resolveAlias": {
		"@/*": "/*"
	},
	"permission": {
		"scope.userLocation": {
			"desc": "用于获取打卡位置"
		}
	},
	"requiredPrivateInfos": [
		"getLocation",
		"startLocationUpdate",
		"onLocationChange"
	],
	"sitemapLocation": "sitemap.json",
	"plugins": {
		"ocr-plugin": {
			"version": "3.1.5",
			"provider": "wx4418e3e031e551be"
		}
	},
	"globalStyle": {
		"backgroundTextStyle": "light",
		"navigationBarTitleText": "小橙长护护理端",
		"navigationBarBackgroundColor": "#1485EE",
		"navigationBarTextStyle": "white",
		"usingComponents": {
			"van-action-sheet": "./wxcomponents/vant/action-sheet/index",
			"van-area": "./wxcomponents/vant/area/index",
			"van-button": "./wxcomponents/vant/button/index",
			"van-card": "./wxcomponents/vant/card/index",
			"van-cell": "./wxcomponents/vant/cell/index",
			"van-cell-group": "./wxcomponents/vant/cell-group/index",
			"van-checkbox": "./wxcomponents/vant/checkbox/index",
			"van-checkbox-group": "./wxcomponents/vant/checkbox-group/index",
			"van-col": "./wxcomponents/vant/col/index",
			"van-count-down": "./wxcomponents/vant/count-down/index",
			"van-calendar": "./wxcomponents/vant/calendar/index",
			"van-dialog": "./wxcomponents/vant/dialog/index",
			"van-divider": "./wxcomponents/vant/divider/index",
			"van-empty": "./wxcomponents/vant/empty/index",
			"van-field": "./wxcomponents/vant/field/index",
			"van-goods-action": "./wxcomponents/vant/goods-action/index",
			"van-goods-action-icon": "./wxcomponents/vant/goods-action-icon/index",
			"van-goods-action-button": "./wxcomponents/vant/goods-action-button/index",
			"van-icon": "./wxcomponents/vant/icon/index",
			"van-image": "./wxcomponents/vant/image/index",
			"van-loading": "./wxcomponents/vant/loading/index",
			"van-nav-bar": "./wxcomponents/vant/nav-bar/index",
			"van-notice-bar": "./wxcomponents/vant/notice-bar/index",
			"van-notify": "./wxcomponents/vant/notify/index",
			"van-panel": "./wxcomponents/vant/panel/index",
			"van-popup": "./wxcomponents/vant/popup/index",
			"van-progress": "./wxcomponents/vant/progress/index",
			"van-radio": "./wxcomponents/vant/radio/index",
			"van-radio-group": "./wxcomponents/vant/radio-group/index",
			"van-row": "./wxcomponents/vant/row/index",
			"van-search": "./wxcomponents/vant/search/index",
			"van-sidebar": "./wxcomponents/vant/sidebar/index",
			"van-sidebar-item": "./wxcomponents/vant/sidebar-item/index",
			"van-slider": "./wxcomponents/vant/slider/index",
			"van-stepper": "./wxcomponents/vant/stepper/index",
			"van-steps": "./wxcomponents/vant/steps/index",
			"van-sticky": "./wxcomponents/vant/sticky/index",
			"van-submit-bar": "./wxcomponents/vant/submit-bar/index",
			"van-swipe-cell": "./wxcomponents/vant/swipe-cell/index",
			"van-uploader": "./wxcomponents/vant/uploader/index",
			"van-switch": "./wxcomponents/vant/switch/index",
			"van-tab": "./wxcomponents/vant/tab/index",
			"van-tabs": "./wxcomponents/vant/tabs/index",
			"van-tabbar": "./wxcomponents/vant/tabbar/index",
			"van-tabbar-item": "./wxcomponents/vant/tabbar-item/index",
			"van-tag": "./wxcomponents/vant/tag/index",
			"van-toast": "./wxcomponents/vant/toast/index",
			"van-transition": "./wxcomponents/vant/transition/index",
			"van-tree-select": "./wxcomponents/vant/tree-select/index",
			"van-datetime-picker": "./wxcomponents/vant/datetime-picker/index",
			"van-rate": "./wxcomponents/vant/rate/index",
			"van-collapse": "./wxcomponents/vant/collapse/index",
			"van-collapse-item": "./wxcomponents/vant/collapse-item/index",
			"van-picker": "./wxcomponents/vant/picker/index",
			"van-overlay": "./wxcomponents/vant/overlay/index",
			"van-circle": "./wxcomponents/vant/circle/index",
			"van-index-bar": "./wxcomponents/vant/index-bar/index",
			"van-index-anchor": "./wxcomponents/vant/index-anchor/index",
			"van-grid": "./wxcomponents/vant/grid/index",
			"van-grid-item": "./wxcomponents/vant/grid-item/index",
			"van-dropdown-menu": "./wxcomponents/vant/dropdown-menu/index",
			"van-dropdown-item": "./wxcomponents/vant/dropdown-item/index",
			"van-skeleton": "./wxcomponents/vant/skeleton/index"
		}
	}
};

// 写入到 outputFile
fs.writeFileSync(outputFile, JSON.stringify(finalConfig, null, 2));
console.log('pages.json has been generated!');